package org.example.erat.visualization;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.awt.*;
import java.util.Map;

public class JFreeChartImplementor implements ChartImplementor {
    @Override
    public void createLineChart(Map<Integer, Double> data, String title, String xAxisLabel, String yAxisLabel) {
        // 创建数据集
        XYSeries series = new XYSeries("提交率");
        data.forEach(series::add);

        XYSeriesCollection dataset = new XYSeriesCollection(series);

        // 创建图表
        JFreeChart chart = ChartFactory.createXYLineChart(
                title,
                xAxisLabel,
                yAxisLabel,
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false
        );

        // 设置中文字体
        Font font = new Font("SimHei", Font.PLAIN, 12); // 使用黑体字体

        // 设置标题字体
        chart.setTitle(new TextTitle(title, font));

        // 设置坐标轴字体
        XYPlot plot = chart.getXYPlot();

        // X轴
        NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
        domainAxis.setLabelFont(font);
        domainAxis.setTickLabelFont(font);

        // Y轴
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setLabelFont(font);
        rangeAxis.setTickLabelFont(font);

        // 设置图例字体
        chart.getLegend().setItemFont(font);

        // 显示图表
        ChartFrame frame = new ChartFrame(title, chart);
        frame.pack();
        frame.setVisible(true);
    }
}